package com.goldgov.config;

import com.goldgov.kduck.security.RoleAccessVoter;
import com.goldgov.oauth2filter.PreRequestFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;

import java.util.Collection;

/**
 * Oauth2Filter配置类，用于初始化并加载相关类文件
 */
@Configuration
public class FilterConfig {
    @Bean
    public FilterProperties filterProperties(){
        return new FilterProperties();
    }

    @Bean
    public PreRequestFilter preRequestFilter(){
        return  new PreRequestFilter(filterProperties());
    }
    @Bean
    public RoleAccessVoter roleAccessVoter(){
        return  new RoleAccessVoter(){

            @Override
            public boolean supports(ConfigAttribute attribute) {
                return true;
            }

            @Override
            public int vote(Authentication authentication, Object object, Collection collection) {
                return  !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated() == true ? 1:-1;
            }

            @Override
            public boolean supports(Class clazz) {
                return true;
            }
        };
    }
}
